package com.jiuyin.dianjing.ui.activity.match;


import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.JsonObject;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.api.base.DefaultApiResult;
import com.jiuyin.dianjing.api.enums.ApiEnum;
import com.jiuyin.dianjing.api.interfaces.ApiResultCallback;
import com.jiuyin.dianjing.api.server.ServerApi;
import com.jiuyin.dianjing.app.HelperApplication;
import com.jiuyin.dianjing.event.RefreshEvent;
import com.jiuyin.dianjing.model.MatchApplyListModel;
import com.jiuyin.dianjing.ui.activity.base.BaseActivity;
import com.jiuyin.dianjing.ui.fragment.match.FragmentMatchApplyList;
import com.jiuyin.dianjing.ui.fragment.match.FragmentMatchRecruitmentInformation;
import com.jiuyin.dianjing.util.DisplayUtil;
import com.jiuyin.dianjing.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_APPLY_FOR_ID;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_COMPETITITON_ID;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_STATUS;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TOKEN;

/**
 * 个人参赛申请
 */
public class MatchApplyInfoActivity extends BaseActivity {

    @BindView(R.id.iv_Head)
    ImageView ivHead;
    @BindView(R.id.tv_name_value)
    TextView tvName;
    @BindView(R.id.tv_time_value)
    TextView tvTime;
    @BindView(R.id.tv_id_key)
    TextView tvIdKey;
    @BindView(R.id.tv_id_value)
    TextView tvId;
    @BindView(R.id.tv_phone_value)
    TextView tvPhone;


    private MatchApplyListModel model;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_match_apply_info;
    }

    @Override
    protected void initData() {
        model = (MatchApplyListModel) getIntent().getSerializableExtra("info");
        if (model != null) {
            if (model.tactics.equals("0")) {
                setTitle(R.string.individual_application);
            } else {
                setTitle(R.string.team_application);
            }
        }
    }

    @Override
    protected void initView() {
        if (model != null) {
            Glide.with(this)
                    .load(model.header)
                    .override(DisplayUtil.dp2px(this, 50))
                    .into(ivHead);
            tvName.setText(model.nickname);
//            tvTime.setText(model.create_time);
            tvPhone.setText(model.mobile);
        }
    }

    @OnClick({R.id.btn_refuse, R.id.btn_agree})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_refuse:
                teamApplyStatus(2);
                break;
            case R.id.btn_agree:
                teamApplyStatus(1);
                break;

        }
    }

    private void teamApplyStatus(int status) {
        HashMap<String, Object> map = new HashMap<>();
        map.put(KEY_TOKEN, HelperApplication.getToken());
        map.put(KEY_STATUS, status);
        map.put(KEY_APPLY_FOR_ID, model.applyfor_id);
        map.put(KEY_COMPETITITON_ID, model.competition_id);
        JSONObject data = new JSONObject(map);
        ApiResultCallback request = new DefaultApiResult() {

            @Override
            public void onSuccess(JsonObject data) {
                if (status == 1) {
                    ToastUtil.showShort("已同意");
                } else {
                    ToastUtil.showShort("已拒绝");
                }
                EventBus.getDefault().post(new RefreshEvent(FragmentMatchApplyList.class.getSimpleName()));
                EventBus.getDefault().post(new RefreshEvent(FragmentMatchRecruitmentInformation.class.getSimpleName()));
                finish();
            }

            @Override
            public void onFail(String msg) {
                super.onFail(msg);
                ToastUtil.showShort(msg);
            }

            @Override
            public void onError(String msg) {
                super.onError(msg);
                ToastUtil.showShort(msg);
            }
        };
        ServerApi.post(ApiEnum.UPDATE_APPLY_FOR_COMPETE, data, null, progress, null, request);
    }
}
